package cn.net.ecode.tools.ecodeplatformmaker.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class DBManager {
	public static Connection conn = null;
	public static String ins = "";
	public static String socket = "";
	public static String dbname = "";
	public static String username = "";
	public static String password = "";

//	static {
//
//		try {
//			JAXPConfigurator.configure("f:/proxool.xml", false);
//		} catch (ProxoolException e) {
//			e.printStackTrace();
//		}
//	}

	public String testConnect(String ins, String socket, String dbname,
			String username, String password) {
		this.ins = ins;
		this.socket = socket;
		this.dbname = dbname;
		this.username = username;
		this.password = password;
		String flag = "good";
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver")
					.newInstance();
			String url = "jdbc:oracle:thin:@" + this.ins + ":"
					+ this.socket + ":" + this.dbname + "";
			this.conn = DriverManager.getConnection(url, this.username,
					this.password);
		} catch (SQLException e) {
			e.printStackTrace();
			flag = e.getMessage();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = ex.getMessage();
		}
		return flag;
	}

	public void getConnect() {
		try {
			if(this.conn.isClosed()) {
				Class.forName("oracle.jdbc.driver.OracleDriver")
				.newInstance();
				String url = "jdbc:oracle:thin:@" + this.ins + ":"
				+ this.socket + ":" + this.dbname + "";
				this.conn = DriverManager.getConnection(url, this.username,
						this.password);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void close() {
		try {
			if (!this.conn.isClosed()) {
				this.conn.close();
				// System.out.println("关闭数据库");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取行政区划
	 * 
	 * @return
	 */
	public String getRegionCode() {
		String sql = "select top 1 xzqh,dwmc from system_info";
		String regionCode = "";
		try {
			ResultSet rs = this.conn.createStatement().executeQuery(sql);
			while (rs.next()) {
				regionCode = rs.getString(1) + "_" + rs.getString(2);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return regionCode;
	}

	/**
	 * 获取字段
	 * 
	 * @param tableName
	 * @return
	 */
	public String getColumnName(String tableName) {
		String sql = "select * from syscolumns where id=object_id('"
				+ tableName + "')";
		String columnsString = "";
		try {
			ResultSet rs = this.conn.createStatement().executeQuery(sql);
			while (rs.next()) {
				columnsString += rs.getString(1) + ",";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		columnsString = columnsString.substring(0, columnsString.length() - 1);
		return columnsString;
	}

	/**
	 * 获取记录
	 * 
	 * @param sql
	 * @param columnCount
	 * @return
	 */
	public ArrayList excecuteQuery(String sql, int columnCount) {
		ArrayList valueList = new ArrayList();
		String valuesString = "";
		int count = 0;
		try {
			ResultSet rs = this.conn.createStatement().executeQuery(sql);
			while (rs.next()) {
				count++;
				for (int i = 0; i < columnCount; i++) {
					if (rs.getString(i + 1) == null) {
						valuesString += "~~";
					} else {
						valuesString += rs.getString(i + 1) + "~~";
					}
				}
				valuesString = valuesString.substring(0,
						valuesString.length() - 2);
				valueList.add(valuesString);
				valuesString = "";
				if(count > 100000) { // wangkun
					break;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return valueList;
	}
	
	public void bkData(String sql)
	{
		PreparedStatement stmt = null;  
		try {
			stmt = conn.prepareStatement(sql);  
			stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}  


		
	}
}
